﻿using System;
using System.Collections.Specialized;
using System.Windows.Input;
using LukasKopenec.LocalizationSupport;
using Microsoft.Win32;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that loads a file into the project resources.
    /// </summary>
    public class NewResourceCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="NewResourceCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// The main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public NewResourceCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            mainWindow.ProjectExplorer.Projects.CollectionChanged += new NotifyCollectionChangedEventHandler(Projects_CollectionChanged);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the collection of projects has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void Projects_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaiseCanExecuteChanged();
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ProjectExplorer.Projects.Count > 0;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var directory = (ViewModel.Directory)parameter;

            var dlg = new OpenFileDialog();

            dlg.AddExtension = true;
            dlg.CheckFileExists = true;
            dlg.Filter = (string)LocalizationManager.GetValue("Application.FileFilters.ImageFiles") + "|" +
                (string)LocalizationManager.GetValue("Application.FileFilters.AudioFiles") + "|" +
                (string)LocalizationManager.GetValue("Application.FileFilters.TextFiles") + "|" +
                (string)LocalizationManager.GetValue("Application.FileFilters.VideoFiles") + "|" +
                (string)LocalizationManager.GetValue("Application.FileFilters.AllFiles");
            bool? result = dlg.ShowDialog();

            if (result == true)
            {
                Mouse.OverrideCursor = Cursors.Wait;

                var project = ApplicationWindow.ProjectExplorer.Projects[0];
                var cmd = new ViewModel.NewResourceCommand(project.UndoStack, project.RedoStack)
                {
                    FileName = dlg.FileName,
                    Parent = directory,
                    Project = ApplicationWindow.ProjectExplorer.Projects[0]
                };
                cmd.Execute(null);

                Mouse.OverrideCursor = null;
            }
        }

        #endregion
    }
}
